package cc.magicjson.reactor;

import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

import java.io.IOException;
import java.time.Duration;


/**
 * {@link Flux}
 *
 * @author <a href="mailto:it_magicjson@163.com">MagicJson</a>
 * @since 1.0.0
 */
public class FluxDemo {

    public static void main(String[] args) throws IOException, InterruptedException {
//        // 定义Flux supplier 角色进行发布/生产
        Flux<Integer> integerFlux = Flux.range(1,7)
                .log()
                .doOnNext(System.out::println)
                .delayElements(Duration.ofSeconds(1))
                .doOnComplete(()-> System.out.println("Flux流正常结束"))
                .doOnCancel(() -> System.out.println("Flux流取消了"));
        // 进行订阅/消费
        integerFlux.subscribe(System.out::println);
        Thread.sleep(2000);
        int read = System.in.read();
        System.out.printf("read:" + read);
        System.getProperties()
                .entrySet()
                .stream()
                .filter(entry -> ((String)entry.getKey()).contains("java"))
                .forEach(System.out::println);

        NullPointerException nullPointerException = new NullPointerException();
        throwException(nullPointerException);

    }

    public static void throwException(Throwable throwable){
        if(throwable instanceof  NullPointerException nullPointerException){
            System.out.printf("======="+throwable);
        }
    }
}
